import yaml
from selenium import webdriver

from src.day36.practice.main_pages import Mainpages
from src.day36.utils import root_dir


class Login:
    __WEBSITE="https://work.weixin.qq.com/wework_admin/loginpage_wx"
    def __init__(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(2)
        self.driver.maximize_window()
        self.driver.get(self.__WEBSITE)

    def login_by_cookies(self):
        """
        通过cookies进行登录
        :return:
        """
        # 2. 读取Cookie信息
        cookies = self.read_cookies_from_file()
        # 3. 把Cookie信息添加到浏览器
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        # 4. 刷新页面
        self.driver.refresh()
        return Mainpages(self.driver)


    def read_cookies_from_file(self):
        with open(f"{root_dir}/data/cookies.yaml", encoding="utf-8") as f:
            cookies = yaml.safe_load(f)
        return cookies
